<?php
class ItemsController extends LilcartAppController {

	var $name = 'Items';

	var $paginate = array(
		'limit' => 10,
		'order' => array(
			'Item.created' => 'desc'
		)
	);

	function admin_index() {
		$this->Item->recursive = 0;
		$this->set('data', $this->paginate());
	}
	
	function admin_add() {
		$this->setAction('admin_edit');
	}
	
	function admin_edit($id = null) {
		$lang = @$this->params['named']['lang'];
		$this->Category->locale = Configure::read('LilcartPlugin.languages');
		
		if (!empty($this->data)) {			
			if ($this->Item->save($this->data)) {
				$this->Session->setFlash(__('The Item has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Item could not be saved. Please, try again.', true), 'error');
			}
		} else if (is_numeric($id)) {
  			$this->data = $this->Item->read(null, $id);
  			
  			// set current language
			if (!empty($lang)) {
				if ($title = Set::extract('/Title[locale='.$lang.']/content', $this->data))
					$this->data['Item']['title'] = $title[0];
					
				if ($descript = Set::extract('/Descript[locale='.$lang.']/content', $this->data))
					$this->data['Item']['descript'] = $descript[0];
			}
		}
		
		$this->set('categories', $this->Item->Category->generateTreeList());
	}
	
	function admin_delete($id = null) {
		if (is_numeric($id) && $this->Item->delete($id)) {
			$this->Session->setFlash(__('Item has been deleted.', true));
			$this->redirect(array('action'=>'admin_index'));
		} else {
			$this->error404();
		}
	}
}
?>